package com.zhifei.system.service.impl;
import java.util.Date;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhifei.system.domain.OnesProjectEntity;
import com.zhifei.system.domain.OnesProjectManhoursEntity;
import com.zhifei.system.domain.OnesProjectMemberEntity;
import com.zhifei.system.mapper.OnesProjectManhoursMapper;
import com.zhifei.system.mapper.OnesProjectMemberMapper;
import com.zhifei.system.service.OnesProjectManhoursService;
import com.zhifei.system.service.OnesProjectMemberService;
import com.zhifei.system.util.ones.OnesProjectMemberInterfaceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OnesProjectMemberServiceImpl extends ServiceImpl<OnesProjectMemberMapper, OnesProjectMemberEntity> implements OnesProjectMemberService {

    @Autowired
    private OnesProjectMemberMapper onesProjectMemberMapper;

    @Override
    public void saveProjectMember(String projectId,String projectMembers) {
        List<OnesProjectMemberInterfaceVo> projectMembersList = JSONObject.parseArray(projectMembers, OnesProjectMemberInterfaceVo.class);
        projectMembersList = projectMembersList.stream().filter(member -> "normal".equals(member.getStatus())).collect(Collectors.toList());
        Integer count = this.lambdaQuery().eq(OnesProjectMemberEntity::getProjectId,projectId).count();
        if(projectMembersList.size()!=count){//如果成员有变化，先删除，重新新增
            LambdaQueryWrapper<OnesProjectMemberEntity> wrapper = new LambdaQueryWrapper<OnesProjectMemberEntity>();
            wrapper.eq(OnesProjectMemberEntity::getProjectId,projectId);
            onesProjectMemberMapper.delete(wrapper);
            List<OnesProjectMemberEntity> onesProjectMemberEntityList = new ArrayList<>();
            for(OnesProjectMemberInterfaceVo onesProjectMemberInterfaceVo : projectMembersList){
                OnesProjectMemberEntity onesProjectMemberEntity = new OnesProjectMemberEntity();
                onesProjectMemberEntity.setProjectId(projectId);
                onesProjectMemberEntity.setUserId(onesProjectMemberInterfaceVo.getUuid());
                onesProjectMemberEntity.setUserEmail(onesProjectMemberInterfaceVo.getEmail());
                onesProjectMemberEntity.setUserName(onesProjectMemberInterfaceVo.getName());
                onesProjectMemberEntity.setUserPost(onesProjectMemberInterfaceVo.getTitle());
                onesProjectMemberEntityList.add(onesProjectMemberEntity);
            }
            this.saveBatch(onesProjectMemberEntityList);
        }

    }
}
